package com.popo.android.lockscreen.impl.httptool;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;

import com.popo.android.lockscreen.imp.HttpTools;

/**
 * 
 * @author popo
 * 
 */
public class ApacheHttpClientUrlTool implements HttpTools {
	HttpClient httpClient;

	public ApacheHttpClientUrlTool() {
		super();
		
	}

	
	public InputStream doPost(String url, String data, File[] files)
			throws Exception {
		// TODO Auto-generated method stub
		org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
		PostMethod postMethod = new PostMethod(url);
		Part[] part = null;
		int partNumbers = 0;
		if (data != null)
			partNumbers ++;
		if (files != null)
			partNumbers += files.length;

		if (partNumbers > 0)
			part = new Part[partNumbers];

		int partkey = 0;

		// 填充参数
		if (data != null) {
			
			
		}
		// 填充附件
		if (files != null) {
			int length = files.length;
			for (int i = 0; i < length; i++) {
				File file = files[i];
				try {
					part[i++] = new FilePart(file.getName(), file);
				} catch (FileNotFoundException ex) {
					throw (ex);
				}
			}
		}

		httpClient.getParams().setContentCharset("UTF-8");
		/**
		 * part如果有空的话会报错处理下吧
		 */
		MultipartRequestEntity mrp = null;
		if (partkey < part.length) {

			Part[] part2 = new Part[partkey];
			for (int i = 0; i < partkey; i++) {
				part2[i] = part[i];
			}
			mrp = new MultipartRequestEntity(part2, postMethod.getParams());
		} else {
			mrp = new MultipartRequestEntity(part, postMethod.getParams());
		}
		postMethod.setRequestEntity(mrp);
		httpClient.executeMethod(postMethod);
		part = null;
		return postMethod.getResponseBodyAsStream();
	}

	public static void main(String[] args) {
		ApacheHttpClientUrlTool a  = new ApacheHttpClientUrlTool();
		InputStream s;
		try {
			s = a.doPost("http://localhost/",null,null);
			System.out.println(a.doin(s));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private String doin(InputStream inputStream){
		StringBuilder stringBuilder = new StringBuilder();
		try {

			byte[] buffer = new byte[1024 * 8];
			BufferedInputStream bufferedInputStream = new BufferedInputStream(
					inputStream);

			while (bufferedInputStream.read(buffer) != -1) {
				stringBuilder.append(new String(buffer));
			}
			buffer = null;
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw (e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				inputStream.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		return stringBuilder.toString();
	}
	@Override
	public String doGetResponseMessage(String url) {
		// TODO Auto-generated method stub
		return null;
	}
}
